from control.models import *
from utils.encryptionAndDecryptionUtil import *
import glob
import pandas as pd
def batch_create_user():
    matched_files = glob.glob("../PCdata软件推广用户信息收集表-总表.xlsx")
    latest_file = max(matched_files, key=lambda x: x.split("-")[-1]) if matched_files else None
    excel_file = pd.ExcelFile(latest_file) 
    sheet_names = excel_file.sheet_names

    for sheet_name in sheet_names:
        print(f"\nSheet Name: {sheet_name}")
        df = pd.read_excel(excel_file, sheet_name=sheet_name, header=1, engine='openpyxl')
        for index, row in df.iterrows():
            User.objects.create(
                **{
                    "user_account": row["账号"],
                    "user_name": row["用户名"],
                    "tel": row["联系电话"],
                    "password": encryptor_psd(row["密码"]),
                    "email": row["邮箱"],
                    "user_type": "general_user",
                    "user_company" : row["公司/组织名称"]
                }
            )
# 初始化默认的用户信息进入系统
def init_user_content():

    super_user_data = {}
    super_user_data["user_account"] = "basic_superadmin"
    super_user_data["user_name"] = "默认超级管理员"
    super_user_data["password"] = encryptor_psd("!SuperAdmin1234")
    super_user_data["user_type"] = "super_admin"
    super_user_data["user_company"] = "默认公司"

    super_admin_entity = User.objects.create(**super_user_data)

    # 初始化超级管理员
    super_user_data = {}
    super_user_data["user_account"] = "superadmin"
    super_user_data["user_name"] = "默认超级管理员"
    super_user_data["password"] = encryptor_psd("!SuperAdmin1234")
    super_user_data["user_type"] = "super_admin"
    super_user_data["user_company"] = "默认公司"

    super_admin_entity = User.objects.create(**super_user_data)

    # 初始化其他角色
    User.objects.create(
        **{
            "user_account": "platformadmin",
            "user_name": "默认平台管理员",
            "password": encryptor_psd("!platform12"),
            "user_type": "platform_admin",
            "user_company" : "默认公司"
        }
    )

    User.objects.create(
        **{
            "user_account": "dataadmin",
            "user_name": "默认数据管理员",
            "password": encryptor_psd("!dataadmin12"),
            "user_type": "data_admin",
            "user_company" : "默认公司"
        }
    )

    User.objects.create(
        **{
            "user_account": "datasetreviewer",
            "user_name": "默认数据集审核员",
            "password": encryptor_psd("!datasetreviewer12"),
            "user_type": "dataset_reviewer",
            "user_company" : "默认公司"
        }
    )

    User.objects.create(
        **{
            "user_account": "model_param_admin",
            "user_name": "默认模型参数管理员",
            "password": encryptor_psd("!modelparam12"),
            "user_type": "model_param_admin",
            "user_company" : "默认公司"
        }
    )

    User.objects.create(
        **{
            "user_account": "generaluser",
            "user_name": "默认一般用户",
            "password": encryptor_psd("!generaluser12"),
            "user_type": "general_user",
            "user_company" : "默认公司"
        }
    )
    print("默认用户初始化完毕")
